﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_OrganizationForm_T : BaseBLL<Model.Order_OrganizationForm_T>, IBLL.IOrder_OrganizationForm_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        public string OrgCountDiscount(string[] DishIDs, int[] DistCount, string MerID, string YZM, int RETID)
        {
            Model.OrderTotal OT = new OrderTotal();
            try
            {
                #region 计算优惠
                double i_优惠后总价 = 0, i_总价 = 0;
                List<string> DishIDsList = new List<string>();
                List<int> DistCountList = new List<int>();
                string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                int day = int.Parse(time.Substring(0, 8));
                string tm = time.Substring(8, 4);
                List<Model.Order_Shelves_T> ShelList = base.DBSession.IOrder_Shelves_TDAL.GetListBy("SELECT * FROM Order_Shelves_T where SheDateB<=" + day + " and SheDateE>=" + day + " and MerID='" + MerID + "' and  SheTimeB<'" + tm + "' and SheTimeE > '" + tm + "' and SheState=1");
                if (ShelList.Count > 0)
                {
                    List<string> rq = new List<string>();
                    List<string> sj = new List<string>();
                    for (int i = 0; i < ShelList[0].MaDay; i++)
                    {
                        rq.Add(DateTime.Now.AddDays(i).ToString("yyyy-MM-dd"));
                    }
                    OT.DeliveryDate = rq;
                    if (ShelList[0].MaTime != "0|0")
                    {
                        string[] times = ShelList[0].MaTime.Split('|');
                        int ks = int.Parse(times[0].Substring(0, 2));
                        int js = int.Parse(times[1].Substring(0, 2));
                        DateTime ks1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, ks, int.Parse(times[0].Substring(2, 2)), 0);
                        DateTime js1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, js, int.Parse(times[1].Substring(2, 2)), 0);
                        if (DateTime.Now < js1)
                        {
                            DateTime ksdt = DateTime.Now.AddMinutes(base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerID == MerID).First().DelTime);
                            if (ks1 < ksdt)
                            {
                                sj.Add(ksdt.ToString("HH:mm"));
                                int s = ksdt.Minute;
                                if (s > 0 & s < 15)
                                {
                                    ksdt = ksdt.AddMinutes(15 - s);
                                }
                                if (s > 15 & s < 30)
                                {
                                    ksdt = ksdt.AddMinutes(30 - s);
                                }
                                if (s > 30 & s < 45)
                                {
                                    ksdt = ksdt.AddMinutes(45 - s);
                                }
                                if (s > 45 & s <= 59)
                                {
                                    ksdt = ksdt.AddMinutes(60 - s);
                                }
                              
                            }
                            else
                            {
                                ksdt = ks1;
                            }
                            sj.Add(ksdt.ToString("HH:mm"));
                            DateTime jsdt = new DateTime(ksdt.Year, ksdt.Month, ksdt.Day, js, int.Parse(times[1].Substring(2, 2)), 0);
                         
                            while (jsdt > ksdt)
                            {
                                string tim = ksdt.AddMinutes(15).ToString("HH:mm");
                                sj.Add(tim);
                                ksdt = ksdt.AddMinutes(15);
                            }
                        }
                       
                    }
                    else
                    {
                        DateTime ksdt = DateTime.Now.AddMinutes(base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerID == MerID).First().DelTime);
                        sj.Add(ksdt.ToString("HH:mm"));
                    }
                    OT.DeliveryTime = sj;
                }
                for (int i = 0; i < DistCount.Length; i++)
                {
                    if (DistCount[i] != 0)
                    {
                        DishIDsList.Add(DishIDs[i]);
                        DistCountList.Add(DistCount[i]);
                    }
                }
                int[] dic = new int[DistCountList.Count];
                for (int i = 0; i < DistCountList.Count; i++)
                {
                    dic[i] = DistCountList[i];
                }
                List<Model.Order_Dishes_T> Dist = base.DBSession.IOrder_Dishes_TDAL.GetListBy(u => DishIDsList.Contains(u.DishesID));
                OT.DishList = Dist;
                OT.CountArr = dic;
                Model.Order_Mer_T Mer = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerID == MerID).First();
                // Mer.DelFormMon = Mer.DelFormMon / 100;
                double i_商品总价 = 0, i_打包费总价 = 0;
                for (int i = 0; i < Dist.Count; i++)
                {

                    //Dist[i].DishesPay = Dist[i].DishesPay / 100;
                    //Dist[i].DelPackMon = Dist[i].DelPackMon / 100;
                    i_商品总价 += Dist[i].DishesPay * dic[i];
                    i_打包费总价 += Dist[i].DelPackMon * dic[i];

                }
                i_总价 = i_商品总价 + i_打包费总价;
                i_总价 += Mer.DelFormMon;
                i_优惠后总价 = i_总价;
                // OT.TotalPoint = i_消费积分.ToString();

                #endregion
                OT.Total = i_优惠后总价.ToString();
                OT.Discount = ((i_总价 - i_优惠后总价)).ToString();
                OT.DelMonTxt = Mer.DelMonTxt;
            }
            catch (Exception)
            {
                result.BS = "-99";
                result.Msg = "系统异常";
            }
            result.BS = "1";
            result.Msg = "读取成功";
            result.rows = OT;
            return os.StringOfObject(result, RETID);
        }
        public string OrgBookOrder(string Data, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                double i_总价 = 0;
                Model.Order_OrganizationForm_T info = ParseClass<Model.Order_OrganizationForm_T>.StrDeserialize(Data, RETID);
                Model.Order_Mer_T Mer = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerID == info.MerID).First();
                string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                int day = int.Parse(time.Substring(0, 8));
                int ti = int.Parse(time.Substring(8, 4));
                string[] DishIDs = info.OrgFormDishesID.Split(',');
                string[] DistCount = info.OrgFormDishesNun.Split(',');
                for (int i = 0; i < DistCount.Length; i++)
                {
                    if (DistCount[i] == "0")
                    {
                        result.BS = "-21";
                        result.Msg = "菜品数量不能为0";
                        return os.StringOfObject(result, RETID);
                    }
                }
                List<Model.Order_Shelves_T> ShelList = base.DBSession.IOrder_Shelves_TDAL.GetListBy(u => u.SheDateB < day & u.SheDateE > day & u.MerID == info.MerID & u.SheState == 1);
                #region 上架列表
                Model.Order_Shelves_T SheLve = new Model.Order_Shelves_T();
                foreach (var item in ShelList)
                {
                    if (int.Parse(item.SheTimeB) < ti & ti < int.Parse(item.SheTimeE))
                    {
                        SheLve = item;
                        break;
                    }
                }
                #endregion
                #region 菜品
                string[] ShelveDishIDS = SheLve.DishesID.Split(',');
                string[] ShelveNumS = SheLve.DishesNub.Split(',');
                for (int i = 0; i < DishIDs.Length; i++)
                {
                    bool kg_是否启用 = false;
                    for (int z = 0; z < ShelveDishIDS.Length; z++)
                    {
                        if (ShelveDishIDS[z] == DishIDs[i])
                        {
                            if (int.Parse(ShelveNumS[z]) < int.Parse(DistCount[i]))
                            {
                                result.BS = "-2";
                                result.Msg = "菜品不够";
                                return os.StringOfObject(result, RETID);
                            }
                            kg_是否启用 = true;
                        }
                    }
                    if (!kg_是否启用)
                    {
                        result.BS = "-1";
                        result.Msg = "菜品没启用";
                        return os.StringOfObject(result, RETID);
                    }
                }
                #endregion
                #region 计算优惠
                List<Model.Order_Dishes_T> Dist = base.DBSession.IOrder_Dishes_TDAL.GetListBy(u => DishIDs.Contains(u.DishesID));
                double i_商品总价实际价格 = 0, i_打包费总价 = 0;
                string FormDisheName = "", FormDishesMon = "";
                Model.Order_Key_T Key = base.DBSession.IOrder_Key_TDAL.GetListBy(u => u.ComOpen == u.ComOpen).First();

                for (int i = 0; i < Dist.Count; i++)
                {

                    FormDisheName += "," + Dist[i].DishesNmae;
                    FormDishesMon += "," + Dist[i].DishesPay;
                    //i_看看的价格 += Dist[i].DishesMon * int.Parse(DistCount[i]);
                    i_商品总价实际价格 += Dist[i].DishesPay * int.Parse(DistCount[i]);
                    i_打包费总价 += Dist[i].DelPackMon * int.Parse(DistCount[i]);
                }
                info.OrgFormDisheName = FormDisheName.Remove(0, 1);
                info.OrgFormDishesMon = FormDishesMon.Remove(0, 1);
                i_总价 = i_商品总价实际价格 + i_打包费总价;
                i_总价 += Mer.DelFormMon;//配送费 
                #endregion
                info.OrgFormPayMon = (int)i_总价;
                info.OrgFormAllMon = (int)i_总价;
                info.OrgFormID = int.Parse(Help.GetSysKey.GetKey("OrgFormKey"));
                info.DelFormMon = (int)Mer.DelFormMon;
                info.DelPackMon = (int)i_打包费总价;
                info.OrgFormLastTime = time;
                info.OrgFormState = 1;
                info.OrgFormTime = time;

                Model.Order_FlowOrganization_T OFO = new Model.Order_FlowOrganization_T();
                Model.Order_Organization_T OO = base.DBSession.IOrder_Organization_TDAL.GetListBy(u => u.OrgID == info.OrgID).First();

                OFO.OrgBeforeLimit = OO.OrgWaltBalance;
                OO.OrgWaltBalance -= info.OrgFormPayMon;
                if (OO.OrgWaltBalance < 0)
                {
                    result.BS = "-4";
                    result.Msg = "帐号余额不足";
                    return os.StringOfObject(result, RETID);
                }
                OFO.OrgAfterLimit = OO.OrgWaltBalance;
                OFO.MerID = Mer.MerID;
                OFO.FlowOrgID = int.Parse(Help.GetSysKey.GetKey("OrgFlowKey"));
                OFO.OrgID = info.OrgID;
                OFO.OrgFlowType = "1A0002";
                OFO.OrgFormID = info.OrgFormID;
                OFO.OrgFlowTime = time;
                OFO.OrgFlowOper = info.OrgFormLastOper;
                if (base.DBSession.IOrder_Organization_TDAL.Modify(OO) > 0)
                {
                    base.Add(info);
                    base.DBSession.IOrder_FlowOrganization_TDAL.Add(OFO);
                    result.BS = "1";
                    result.Msg = "下单成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "下单不成功";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("企业下订单信息失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统异常";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }
        /// <summary>
        /// 获取企业所有订单列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="OrgID">企业账户ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string GetOrderFormOrgID(int pageIndex, int pageSize, int OrgID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.Order_OrganizationForm_T> ls = new List<Model.Order_OrganizationForm_T>();
                int tol = 0;
                ls = base.DBSession.IOrder_OrganizationForm_TDAL.GetPagedList<string>(pageIndex, pageSize, ref tol, u => u.OrgID == OrgID, u => u.OrgFormTime);
                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = ls;
                    result.total = tol;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品分类信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        public string GetOrderFormMerID(int pageIndex, int pageSize, string MerID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                List<Model.Order_OrganizationForm_T> ls = new List<Model.Order_OrganizationForm_T>();
                int tol = 0;
                ls = base.DBSession.IOrder_OrganizationForm_TDAL.GetPagedList<string>(pageIndex, pageSize, ref tol, u => u.MerID == MerID, u => u.OrgFormTime);
                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = ls;
                    result.total = tol;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品分类信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }
    }
}
